﻿using DFGK.IRepository;
using DFGK.IService;
using DFGK.Repository;
using DFGK.Service;
using DFGK_WMS.Common;
using DFGK_WMS.Model.Dto;
using DFGK_WMS.Model.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore.Metadata.Internal;

namespace DFGK_WMS.WebAPI.Controllers.PDA.出库单管理
{
    /// <summary>
    /// 出库管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [ApiExplorerSettings(GroupName = "PDA")]
    public class ShippingOrderDetailsController : BaseController
    {
        private readonly IPutawayOrderRepository putawayOrderRepository;
        private readonly IShippingOrderDetailService shippingOrderDetailService;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="environment"></param>
        /// <param name="shippingOrderDetailService"></param>
        public ShippingOrderDetailsController(IWebHostEnvironment environment, IShippingOrderDetailService shippingOrderDetailService, IPutawayOrderRepository putawayOrderRepository) : base(environment)
        {
            this.putawayOrderRepository = putawayOrderRepository;
            this.shippingOrderDetailService = shippingOrderDetailService;
        }

        /// <summary>
        /// 手持出库选择出库单
        /// </summary>
        /// <param name="key"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetShippingOrderDetailGroup(string? key, int pageIndex, int pageSize)
        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                return Ok(BaseResult.Result("400", "分页参数有误！"));
            }
            int totalCount;
            var list = shippingOrderDetailService.GetShippingOrderDetailGroup(key, pageIndex, pageSize, out totalCount);
            if (list != null)
            {
                return Ok(BaseResult.Result(list, "查询成功！", "200", totalCount));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));
        }

        /// <summary>
        /// 开始出库
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult StartShippingOrderDetail(List<SubmitOutListDTO> m)
        {
            if (!m.Any())
            {
                return Ok(BaseResult.Result("400", "分页参数有误！"));
            }
            foreach (var item in m) 
            {
                item.NowUserId = base.BaseGetNowUserId();
                item.NowUserName = base.BaseGetNowUserName();
            }
            string message;
            var putobj = putawayOrderRepository.QueryEntityWhere(x => x.Status != 5);
            if (putobj != null)
                return Ok(BaseResult.Result(false, "保存失败！有入库任务正在执行！", "500"));
            var list = shippingOrderDetailService.GetCellDepth(m);
            bool result = shippingOrderDetailService.StartShippingOrderDetail(list, out message);
            if (result)
            {
                return Ok(BaseResult.Result(result, "操作成功！", "200"));
            }
            return Ok(BaseResult.Result(result, "操作失败！" + message, "500"));
        }

        /// <summary>
        /// 拣选出库回流
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult StartPickingShippingOrderDetail(StartInventoryDTO m)
        {
            if (m == null)
            {
                return Ok(BaseResult.Result("400", "分页参数有误！"));
            }
            string message;
            bool result = shippingOrderDetailService.StartPickingShippingOrderDetail(m, out message);
            if (result)
            {
                return Ok(BaseResult.Result(result, "添加成功！", "200"));
            }
            return Ok(BaseResult.Result(result, "添加失败！" + message, "500"));
        }

        /// <summary>
        /// 拣选出库查询数据
        /// </summary>
        /// <param name="PalletCode"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetReceivingRoderData(string PalletCode)
        {
            if (string.IsNullOrEmpty(PalletCode))
            {
                return Ok(BaseResult.Result("400", "托盘不能为空！"));
            }
            var obj = shippingOrderDetailService.GetReceivingRoderData(PalletCode, out string message);
            if (obj != null)
            {
                return Ok(BaseResult.Result(obj, "查询成功！", "200", 0));
            }
            return Ok(BaseResult.Result(null, "查询失败！" + message, "500", 0));
        }

        /// <summary>
        /// 拣选入库呼叫库存中箱体
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult StartShippingOutZeroQty(List<SubmitOutListDTO> m)
        {
            if (!m.Any())
            {
                return Ok(BaseResult.Result("400", "分页参数有误！"));
            }
            string message;
            bool result = shippingOrderDetailService.StartShippingOutZeroQty(m, out message);
            if (result)
            {
                return Ok(BaseResult.Result(result, "操作成功！", "200"));
            }
            return Ok(BaseResult.Result(result, "操作失败！" + message, "500"));
        }
        #region 弃用
        /// <summary>
        /// 打包区
        /// </summary>
        /// <param name="PalletCode"></param>
        /// <returns></returns>
        [HttpGet, Obsolete("该方法已被已弃用")]
        public IActionResult UpdShippingLockCellStatus2(string PalletCode)
        {
            //if (string.IsNullOrEmpty(PalletCode))
            //{
            //    return Ok(BaseResult.Result("400", "分页参数有误！"));
            //}
            //string message = "";
            //bool IsOK = shippingOrderDetailService.UpdShippingLockCellStatus2(PalletCode, out message);
            //if (IsOK)
            //{
            //    return Ok(BaseResult.Result(true, "操作成功！", "200"));
            //}
            //return Ok(BaseResult.Result(false, "操作失败！" + message, "500"));
            return Ok("弃用了你看不到吗？还点");
        }

        /// <summary>
        /// 缓存区
        /// </summary>
        /// <param name="PalletCode"></param>
        /// <returns></returns>
        [HttpGet, Obsolete("该方法已被已弃用")]
        public IActionResult UpdShippingLockCellStatus4(string PalletCode)
        {
            //if (string.IsNullOrEmpty(PalletCode))
            //{
            //    return Ok(BaseResult.Result("400", "分页参数有误！"));
            //}
            //string message = "";
            //bool IsOK = shippingOrderDetailService.UpdShippingLockCellStatus4(PalletCode, out message);
            //if (IsOK)
            //{
            //    return Ok(BaseResult.Result(true, "操作成功！", "200"));
            //}
            //return Ok(BaseResult.Result(false, "操作失败！" + message, "500"));
            return Ok("弃用了你看不到吗？还点");
        }

        /// <summary>
        /// 根据托盘获取信息
        /// </summary>
        /// <param name="PalletCode"></param>
        /// <returns></returns>
        [HttpGet, Obsolete("该方法已被已弃用")]
        public IActionResult GetPackCachePort(string PalletCode)
        {
            //if (string.IsNullOrEmpty(PalletCode))
            //{
            //    return Ok(BaseResult.Result("400", "分页参数有误！"));
            //}
            //string message;
            //var obj = shippingOrderDetailService.GetPackCachePort(PalletCode,out message);
            //if (obj != null)
            //{
            //    return Ok(BaseResult.Result(obj, "查询成功！", "200", 0));
            //}
            //return Ok(BaseResult.Result(null, "查询失败！"+ message, "500", 0));
            return Ok("弃用了你看不到吗？还点");
        }

        /// <summary>
        /// 根据托盘获取暂存区信息
        /// </summary>
        /// <param name="PalletCode"></param>
        /// <returns></returns>
        [HttpGet, Obsolete("该方法已被已弃用")]
        public IActionResult GetPackCachePort2(string PalletCode)
        {
            //if (string.IsNullOrEmpty(PalletCode))
            //{
            //    return Ok(BaseResult.Result("400", "分页参数有误！"));
            //}
            //string message;
            //var obj = shippingOrderDetailService.GetPackCachePort2(PalletCode, out message);
            //if (obj != null)
            //{
            //    return Ok(BaseResult.Result(obj, "查询成功！", "200", 0));
            //}
            //return Ok(BaseResult.Result(null, "查询失败！" + message, "500", 0));
            return Ok("弃用了你看不到吗？还点");
        }
        #endregion

        /// <summary>
        /// 打包区显示
        /// </summary>
        /// <param name="order"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetShippingLockCellPackList(string? order = "", int pageIndex = 1, int pageSize = 10)
        {
            //return Ok("弃用了你看不到吗？还点");
            if (pageIndex <= 0 || pageSize <= 0)
            {
                return Ok(BaseResult.Result("400", "分页参数有误！"));
            }
            int totalCount;
            var list = shippingOrderDetailService.GetShippingLockCellPackList(order,pageIndex, pageSize, out totalCount);
            return Ok(BaseResult.Result(list, "查询成功！", "200", totalCount));
        }

        /// <summary>
        /// 暂存区显示
        /// </summary>
        /// <param name="order"></param>
        /// <param name="status"></param>
        /// <param name="startdate"></param>
        /// <param name="enddate"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetShippingLockCellCachePortList(string? order, int? status, DateTime startdate, DateTime enddate, int pageIndex, int pageSize)
        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                return Ok(BaseResult.Result("400", "分页参数有误！"));
            }
            int totalCount;
            var list = shippingOrderDetailService.GetShippingLockCellCachePortList(order,status,startdate,enddate, pageIndex, pageSize, out totalCount);
            return Ok(BaseResult.Result(list, "查询成功！", "200", totalCount));
        }

        /// <summary>
        /// 打包区-根据出库单明细的Id进行操作
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult UpdShippingLockCellStatus2q(List<ShippingLockPackDto> ids)
        {
            //return Ok("弃用了你看不到吗？还点");
            if (!ids.Any())
            {
                return Ok(BaseResult.Result("400", "分页参数有误！"));
            }
            string message = "";
            bool IsOK = shippingOrderDetailService.UpdShippingLockCellStatus2(ids, out message);
            if (IsOK)
            {
                return Ok(BaseResult.Result(true, "操作成功！", "200"));
            }
            return Ok(BaseResult.Result(false, "操作失败！" + message, "500"));
        }

        /// <summary>
        /// 缓存区-根据出库单明细的Id进行操作
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult UpdShippingLockCellStatus4q(List<long> ids)
        {
            //return Ok("弃用了你看不到吗？还点");
            if (!ids.Any())
            {
                return Ok(BaseResult.Result("400", "分页参数有误！"));
            }
            string message = "";
            bool IsOK = shippingOrderDetailService.UpdShippingLockCellStatus4(ids, out message);
            if (IsOK)
            {
                return Ok(BaseResult.Result(true, "操作成功！", "200"));
            }
            return Ok(BaseResult.Result(false, "操作失败！" + message, "500"));
        }
    }
}
